/**
 * @author caojj
 * @version 1.0.2.5
 */

FWK = window.FWK || {};

/**
 * 追加样式
 * @param {Element} element
 * @param {String} className
 * @param {Boolean} keepLocalClass
 */
FWK.attachClass = function(element, className, keepLocalClass) {
	className = className.replace(/^\s+|\s+$/g,"");
	var elmClass = element.className;
	var reg = new RegExp("\\b"+className+"\\b", "g");
	if (elmClass.search(reg)<0) {		
		elmClass = elmClass.length <= 0 ? [] : elmClass.split(" ");
		
		if (!keepLocalClass) {
			if (elmClass.length) {
				elmClass = [elmClass[0]];
			}
		}
		
		elmClass.push(className);
		elmClass = elmClass.join(" ");
	}
	element.className = elmClass.replace(/\s+/g,' ');
	
	return elmClass;
}

/**
 * 剔除样式
 * @param {Element} element
 * @param {String} className
 */
FWK.detachClass = function(element, className) {	
	if (!className) {
		elmClass = element.className.split(" ")[0];
	} else {
		className = className.replace(/^\s+|\s+$/g,"");
		var reg = new RegExp("\\b"+className+"\\b", "g");
		var elmClass = element.className.replace(reg, "");	
	}
	element.className = elmClass;
	
	return elmClass;
}

/**
 * 根据属性名从界面获取属性的值
 * @param {Object} elem
 */
FWK.getInputValue = function(elem){
	if (typeof(elem)=="string") {
		elem = document.getElementsByName(elem);
	}
	
	var value = "";
	
	if (elem && elem[0]) {
		var type = elem[0].type || elem.type;
		//				alert(type)
		
		switch (type) {
			case "radio":
				var len = elem.length;
				for (var i = 0; i < len; i++) {
					if (elem[i].checked) {
						value = elem[i].value;
						break;
					}
				}
				break;
			case "checkbox":
				var tmp = [];
				var len = elem.length;
				for (var i = 0; i < len; i++) {
					if (elem[i].checked) {
						tmp.push(elem[i].value);
					}
				}
				value = tmp.join();
				break;
			case "select-one":
				value = elem.value||elem[0].value;
				break;
			default:
				var tmp = [];		
				var len = elem.length;
				for (var i = 0; i < len; i++) {
					tmp.push(elem[i].value);
				}
				value = tmp.join();
				break;
		}
	}
	return value;
};

/**
 * 将值赋给界面的属性
 * @param {Object} elem
 * @param {Object} value
 */
FWK.setInputValue = function(elem, value) {
	if (typeof(elem)=="string") {
		elem = document.getElementsByName(elem);
	}
	
	if (elem && elem[0]) {
		var type = elem[0].type || elem.type;
		//				alert(type)
		
		switch (type) {
			case "radio":
				var len = elem.length;
				for (var i = 0; i < len; i++) {
					if (elem[i].value.toString() == value.toString()) {
						elem[i].checked = true;
						break;
					}
				}
				break;
			case "checkbox":
				var len = elem.length;
				if (value instanceof Array) {
					value = value.join();
				}
				for (var i = 0; i < len; i++) {
					elem[i].checked = (value.search(elem[i].value) > -1);
				}
				break;
			case "select-one":
				if(elem.tagName=="SELECT") {
					elem.value = value;
					try {
						elem.onchange();
					}catch(ex){}
				} else {
					elem[0].value = value;
					try {
						elem[0].onchange();
					}catch(ex){}						
				}
				break;
			default:
				var len = elem.length;
				for (var i = 0; i < len; i++) {
					elem[i].value = value;
				}
		}
	}
};


/**
 * 获取页面QueryString参数集合
 */
FWK.paraStr = location.search.substr(1);
var request = {};
(FWK.getQueryParam = function(key) {
	if (key) {
		return request[key];
	}
	
	if (FWK.paraStr.length>1) {
		paras = FWK.paraStr.split(/\&|\?/);
		for(var idx in paras) {
			var para = paras[idx];
			//if (!para.match(/=\d+$/)) {	//非数字类型加引号
				para = para.replace(/\\/g, "\\");	//斜杠转义
				para = para.replace(/\'/g, "\\'");	//单引号转义
				para = para.replace(/\"/g, '\\"');	//双引号转义
				para = para.replace(/=(.*)/,"=\'$1\'");		//字符串值加引号
			//}

			if (para.indexOf("=")>0) {	//过滤没有表达式的情况(可以不过滤)
				eval("request."+para);						
			}					
		}
	}
	return request;
})();
//getRequestParam();